<?php
namespace app\mini\model;

use app\admin\model\SysModel;
use think\facade\Db;
use app\common\traits\ActionRecordTrait;
use think\Model;
use app\admin\controller\ApiCommon;
use app\crm\model\Service;

class MiniStisticsModel extends Model
{

    /**

     * @var string

     */

    protected $name = 'admin_sys';



    protected $defaultSoftDelete = 0;



    protected $autoWriteTimestamp = true;

    /**

     * 获取新增会员数据

     */
    public function getNewVip($request)
    {
        $st = $request['startTime'];

        $et = $request['endTime'];

        $shopIds = $request['shopIds'];

        $person_id = isset($request['shop_person_id'])?$request['shop_person_id']:0;

        $where = [];

        if(!empty($person_id)){
            array_push($where,['nurse_id','=',$person_id]);
        }

        array_push($where,['shop_id','in',$shopIds]);

        array_push($where,['join_time','between',"$st,$et"]);

        // 新增会员顾客数
        $res = Db::name('customer')->where($where)->count();

        return [

            'count' => $res,

        ];
    }

    /**

     * 获取进店数(来源)

     */
    public function getComeInNum($type,$sourceIds,$request)
    {

        $st = $request['startTime'];

        $et = $request['endTime'];

        $shopIds = $request['shopIds'];

        $person_id = isset($request['shop_person_id'])?$request['shop_person_id']:0;

        $where = [];

        if($person_id > 0){
            array_push($where,['nurse_id','=',$person_id]);
        }

        if($sourceIds){

            if($type == 1){

                array_push($where,['source_id','not in',$sourceIds]);

            }else{

                array_push($where,['source_id','in',$sourceIds]);

            }


        }

        array_push($where,['shop_id','in',$shopIds]);

        array_push($where,['first_time|second_time','between',"$st,$et"]);

        $res = Db::name('customer')->where($where)->count();

        return [

            'count' => $res,

        ];
    }

    /**

     * 获取进店数(疤痘，项目)

     */
    public function getComeInScarOrPorject($scarType,$request)
    {
        $st = $request['startTime'];

        $et = $request['endTime'];

        $shopIds = $request['shopIds'];

        $person_id = isset($request['shop_person_id'])?$request['shop_person_id']:0;

        $where = [];

        if($person_id>0){
            array_push($where,['c.nurse_id','=',$person_id]);
        }

        array_push($where,['c.shop_id','in',$shopIds]);

        array_push($where,['c.first_time|c.second_time','between',"$st,$et"]);

        $res = Db::name('customer_scar')->alias('cs')

            ->join('customer c', 'c.id = cs.customer_id', 'LEFT')

            ->where('cs.customer_setting_id_type','in',$scarType)

            ->where($where)->count();

        return [

            'count' => $res,

        ];
    }


    /**

     * 获取疤痕（项目）成交数-通过成交单子进行判断

     */
    public function getCjNum($business_type,$request)
    {
        $st = $request['startTime'];

        $et = $request['endTime'];

        $shopIds = $request['shopIds'];

        $person_id = isset($request['shop_person_id'])?$request['shop_person_id']:0;

        $where = [];

        if($person_id>0){
            array_push($where,['nurse_id','=',$person_id]);
        }

        array_push($where,['shop_id','in',$shopIds]);

        array_push($where,['cj_time','between',"$st,$et"]);

        $c_list = Db::name('customer')->where($where)->column('id,cj_order_id');

        $orderIds = array_values(array_unique(array_column($c_list,'cj_order_id')));

        $cj_where = [];

        array_push($cj_where,['id','in',$orderIds]);

        array_push($cj_where,['business_type','=',$business_type]);

        $res = Db::name('shop_refund_order')->where($cj_where)->count();

        return [

            'count' => $res,

        ];
    }

    // 新单业绩
    public function newOrder($request)
    {

        $start_time = $request['startTime'];

        $end_time = $request['endTime'];

        $shop_id = $request['shop_id'];

        $staff_ids = $request['staffList'];


        $staff_name = Db::name('shop_person')->where('id','in',$staff_ids)->column('name','id');


        $list1 = Db::name('shop_refund_order')->where([['shop_id', 'in', $shop_id], ['sale_id', 'in', $staff_ids], ['deal_time', 'between', "$start_time,$end_time"], ['type','=',3], ['status','=',2], ['order_type','=',1], ['refund_type','=','101.02 ']])->group('sale_id')->column('sum(pay_amount) as sum','sale_id');

        $list2 = Db::name('shop_refund_order')->where([['shop_id', 'in', $shop_id], ['sale_id', 'in', $staff_ids], ['deal_time', 'between', "$start_time,$end_time"], ['type','=',2], ['status','=',2], ['order_type','=',1], ['refund_type','=','101.02 ']])->group('sale_id')->column('sum(pay_amount) as sum','sale_id');

        $list = [];

        foreach ($staff_ids as $key => $value) {

            $one = [];

            $one['name'] = $staff_name[$value] ?? '';

            $shouKuan = $list1[$value] ?? '0.00';

            $tuiKuan = $list2[$value] ?? '0.00';

            $one['newOrder'] = bcsub($shouKuan,$tuiKuan,2);

            array_push($list,$one);

        }


        $edit = array_column($list,'newOrder');

        array_multisort($edit, SORT_DESC ,$list);

        return $list;
    }

    // 补单业绩
    public function repairOrder($request){

        $start_time = $request['startTime'];

        $end_time = $request['endTime'];

        $shop_id = $request['shop_id'];

        $staff_ids = $request['staffList'];

        $staff_name = Db::name('shop_person')->where('id','in',$staff_ids)->column('name','id');

        $list1 = Db::name('shop_refund_order')->where([['shop_id', 'in', $shop_id], ['sale_id', 'in', $staff_ids], ['deal_time', 'between', "$start_time,$end_time"], ['type','=',3], ['status','=',2], ['order_type','=',2], ['refund_type','=','101.02 ']])->group('sale_id')->column('sum(pay_amount) as sum','sale_id');

        $list2 = Db::name('shop_refund_order')->where([['shop_id', 'in', $shop_id], ['sale_id', 'in', $staff_ids], ['deal_time', 'between', "$start_time,$end_time"], ['type','=',2], ['status','=',2], ['order_type','=',2], ['refund_type','=','101.02 ']])->group('sale_id')->column('sum(pay_amount) as sum','sale_id');

        $list = [];

        foreach ($staff_ids as $key => $value) {

            $one = [];

            $one['name'] = $staff_name[$value] ?? '';

            $shouKuan = $list1[$value] ?? '0.00';

            $tuiKuan = $list2[$value] ?? '0.00';

            $one['repairOrder'] = bcsub($shouKuan,$tuiKuan,2);

            array_push($list,$one);

        }

        $edit = array_column($list,'repairOrder');

        array_multisort($edit, SORT_DESC ,$list);

        return $list;
    }

    // 体验成交
    public function experience($request){

        $start_time = $request['startTime'];

        $end_time = $request['endTime'];

        $shop_id = $request['shop_id'];

        $staff_ids = $request['staffList'];

        $staff_name = Db::name('shop_person')->where('id','in',$staff_ids)->column('name','id');

        $list1 = Db::name('shop_refund_order')->where([['shop_id', 'in', $shop_id], ['is_experience', '=', 1], ['experience_id', 'in', $staff_ids], ['deal_time', 'between', "$start_time,$end_time"], ['type','=',3], ['status','=',2], ['refund_type','=','101.02 ']])->group('experience_id')->column('sum(pay_amount) as sum','experience_id');

        $list2 = Db::name('shop_refund_order')->where([['shop_id', 'in', $shop_id], ['is_experience', '=', 1], ['experience_id', 'in', $staff_ids], ['deal_time', 'between', "$start_time,$end_time"], ['type','=',2], ['status','=',2], ['refund_type','=','101.02 ']])->group('experience_id')->column('sum(pay_amount) as sum','experience_id');

        $list = [];

        foreach ($staff_ids as $key => $value) {

            $one = [];

            $one['name'] = $staff_name[$value] ?? '';

            $shouKuan = $list1[$value] ?? '0.00';

            $tuiKuan = $list2[$value] ?? '0.00';

            $one['experience'] = bcsub($shouKuan,$tuiKuan,2);

            array_push($list,$one);

        }

        $edit = array_column($list,'experience');

        array_multisort($edit, SORT_DESC ,$list);

        return $list;
    }

    // 护理次数
    public function serviceLog($request){

        $service_model = new Service();

        $start_time = $request['startTime'];

        $end_time = $request['endTime'];

        $shop_id = $request['shop_id'];

        $staff_ids = $request['staffList'];

        $staff_name = Db::name('shop_person')->where('id','in',$staff_ids)->column('name','id');

        $category = $service_model->getPushCategoryArray();

        $category_ids =array_column($category,'id');

        $list1 = Db::name('service_log')->where([['shop_id', 'in', $shop_id], ['nurse_id_one', 'in', $staff_ids], ['nurse_id_two', '=', 0], ['create_time', 'between', "$start_time,$end_time"],['category_id','in',$category_ids]])->group('nurse_id_one')->column('count(*) as sum','nurse_id_one');

        // echo "<pre>";
        // print_r($list1);
        // echo "</pre>";
        // exit;

        $list2 = Db::name('service_log')->where([['shop_id', 'in', $shop_id], ['nurse_id_one', 'in', $staff_ids], ['nurse_id_two', '>', 0], ['create_time', 'between', "$start_time,$end_time"],['category_id','in',$category_ids]])->group('nurse_id_one')->column('count(*)*0.5 as sum','nurse_id_one');

        $list3 = Db::name('service_log')->where([['shop_id', 'in', $shop_id], ['nurse_id_one', '>', 0], ['nurse_id_two', 'in', $staff_ids], ['create_time', 'between', "$start_time,$end_time"],['category_id','in',$category_ids]])->group('nurse_id_two')->column('count(*)*0.5 as sum','nurse_id_two');

        $list = [];

        foreach ($staff_ids as $key => $value) {

            $one = [];

            $one['name'] = $staff_name[$value] ?? '';

            $num1 = $list1[$value] ?? '0';

            $num2 = $list2[$value] ?? '0';

            $num3 = $list3[$value] ?? '0';

            $hezuo = bcadd($num2,$num3);

            $one['serviceLog'] = bcadd($num1,$hezuo);

            array_push($list,$one);

        }

        $edit = array_column($list,'serviceLog');

        array_multisort($edit, SORT_DESC ,$list);

        return $list;
    }

    // 护理人数
    public function serviceNums($request){

        $start_time = $request['startTime'];

        $end_time = $request['endTime'];

        $shop_id = $request['shop_id'];

        $staff_ids = $request['staffList'];

        $staff_name = Db::name('shop_person')->where('id','in',$staff_ids)->column('name','id');

        $list = [];


        foreach ($staff_ids as $key => $value) {

            $one = [];

            $c_list = Db::name('service_log')->where([['nurse_id_one|nurse_id_two', '=', $value], ['create_time', 'between', "$start_time,$end_time"]])->select()->toArray();

            $one['name'] = $staff_name[$value] ?? '';

            $one['serviceNums'] = count($c_list) ?? 0;

            array_push($list,$one);

        }



        $edit = array_column($list,'serviceNums');

        array_multisort($edit, SORT_DESC ,$list);

        return $list;

    }

    // 转介绍数
    public function zhuanNums($request){

        $start_time = $request['startTime'];

        $end_time = $request['endTime'];

        $shop_id = $request['shop_id'];

        $staff_ids = $request['staffList'];

        $staff_name = Db::name('shop_person')->where('id','in',$staff_ids)->column('name','id');

        $list1 = Db::name('shop_refund_order')->where([['shop_id', 'in', $shop_id], ['is_introduction', '=', 1], ['introduction_id', 'in', $staff_ids], ['deal_time', 'between', "$start_time,$end_time"], ['type','=',3], ['status','=',2], ['refund_type','=','101.02 ']])->group('introduction_id')->column('sum(pay_amount) as sum','introduction_id');

        $list2 = Db::name('shop_refund_order')->where([['shop_id', 'in', $shop_id], ['is_introduction', '=', 1], ['introduction_id', 'in', $staff_ids], ['deal_time', 'between', "$start_time,$end_time"], ['type','=',2], ['status','=',2], ['refund_type','=','101.02 ']])->group('introduction_id')->column('sum(pay_amount) as sum','introduction_id');

        $list = [];

        foreach ($staff_ids as $key => $value) {

            $one = [];

            $one['name'] = $staff_name[$value] ?? '';

            $shouKuan = $list1[$value] ?? '0.00';

            $tuiKuan = $list2[$value] ?? '0.00';

            $one['zhuanNums'] = bcsub($shouKuan,$tuiKuan,2);

            array_push($list,$one);

        }

        $edit = array_column($list,'zhuanNums');

        array_multisort($edit, SORT_DESC ,$list);

        return $list;

    }

    // 外联人数
    public function waiLianNums($request){

        $start_time = $request['startTime'];

        $end_time = $request['endTime'];

        $shop_id = $request['shop_id'];

        $staff_ids = $request['staffList'];

        $staff_name = Db::name('shop_person')->where('id','in',$staff_ids)->column('name','id');

        $list1 = Db::name('customer')->where([['shop_id', 'in', $shop_id], ['outreach_id', 'in', $staff_ids], ['first_time|second_time', 'between', "$start_time,$end_time"]])->group('outreach_id')->column('count(*) as sum','outreach_id');

        $list = [];

        foreach ($staff_ids as $key => $value) {

            $one = [];

            $one['name'] = $staff_name[$value] ?? '';

            $one['waiLianNums'] = $list1[$value] ?? '0';

            array_push($list,$one);

        }



        $edit = array_column($list,'waiLianNums');

        array_multisort($edit, SORT_DESC ,$list);

        // echo "<pre>";
        // print_r($list);
        // echo "</pre>";
        // exit;

        return $list;
    }

}

?>
